package repositories

import (
	"gin_erik/internal/database"
	"gin_erik/internal/models"
	"gorm.io/gorm"
)

var DB *gorm.DB

type UserRepository struct {
	users []models.User
}

func NewUserRepository() *UserRepository {
	DB = database.X
	return &UserRepository{
		users: []models.User{
			{ID: 1, Name: "Alice"},
			{ID: 2, Name: "Bob"},
		},
	}
}

func (r *UserRepository) GetAllUsers() []models.User {
	x := database.X
	var users []models.User
	tx := x.Find(&users)
	if tx.Error != nil {
		return nil
	}
	r.users = users
	return r.users
}

func (r *UserRepository) GetUserByID(id string) (models.User, error) {
	var user models.User
	tx := DB.First(&user, id)
	if tx.Error != nil {
		return models.User{}, tx.Error
	}
	return user, nil
}
